﻿using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using NewsteadApp_Android.Resources.Activities;
using System.Data.SqlClient;
using System.Data;
using Android.Text.Method;

namespace NewsteadApp_Android
{
    [Activity(Label = "NewsteadApp_Android", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected Button btnSignMn = null;
        protected EditText staffID = null;
        protected TextView TextViewTT = null;
        string SaveStaffID = "";
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetTheme(Android.Resource.Style.ThemeLightNoTitleBarFullScreen);
            // Set our view from the "main" layout resource

            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it 
            var number0 = FindViewById<Button>(Resource.Id.number0);
            var number1 = FindViewById<Button>(Resource.Id.number1);
            var number2 = FindViewById<Button>(Resource.Id.number2);
            var number3 = FindViewById<Button>(Resource.Id.number3);
            var number4 = FindViewById<Button>(Resource.Id.number4);
            var number5 = FindViewById<Button>(Resource.Id.number5);
            var number6 = FindViewById<Button>(Resource.Id.number6);
            var number7 = FindViewById<Button>(Resource.Id.number7);
            var number8 = FindViewById<Button>(Resource.Id.number8);
            var number9 = FindViewById<Button>(Resource.Id.number9);
            var btnClear = FindViewById<Button>(Resource.Id.btnClear);
            var btnEnter = FindViewById<Button>(Resource.Id.btnEnter);
            TextViewTT = FindViewById<TextView>(Resource.Id.TextViewTitle);
            btnSignMn = FindViewById<Button>(Resource.Id.btnSignInMn);
            staffID = FindViewById<EditText>(Resource.Id.txtStaffID);



            number1.Click += (sender, e) =>
            {
                staffID.Text += 1;
            };
            number2.Click += (sender, e) =>
            {
                staffID.Text += 2;
            };
            number3.Click += (sender, e) =>
            {
                staffID.Text += 3;
            };
            number4.Click += (sender, e) =>
            {
                staffID.Text += 4;
            };
            number5.Click += (sender, e) =>
            {
                staffID.Text += 5;
            };
            number6.Click += (sender, e) =>
            {
                staffID.Text += 6;
            };
            number7.Click += (sender, e) =>
            {
                staffID.Text += 7;
            };
            number8.Click += (sender, e) =>
            {
                staffID.Text += 8;
            };
            number9.Click += (sender, e) =>
            {
                staffID.Text += 9;
            };
            number0.Click += (sender, e) =>
            {
                //staffID.Text += 0;
                StartActivity(typeof(CashierActivity));
            };
            btnClear.Click += (sender, e) =>
            {
                staffID.Text = "";
            };
            btnEnter.Click += (sender, e) =>
            {
                if (TextViewTT.Text == "Please Enter Your ID" || TextViewTT.Text == "This User Name Do Not Exist!")
                {
                    CheckStaffID();
                }
                else if (TextViewTT.Text == "Please Enter Password" || TextViewTT.Text == "Password Incorrect")
                {
                    TextViewTT.InputType = Android.Text.InputTypes.NumberVariationPassword;
                    CheckStaffPassword();
                } 
                
            };
            btnSignMn.Click += (sender, e) =>
            {
                StartActivity(typeof(AdminLogin));
            };
        }

        public string Username
        {
            get { return staffID.Text; }
        }

        private void CheckStaffID()
        {
            NewsteadWebReference.NewsteadService CheckExistStaffID = new NewsteadWebReference.NewsteadService();
            string ConfirmLogin = CheckExistStaffID.CheckStaffID(staffID.Text);
            if (ConfirmLogin == "staffExist")
            {
                TextViewTT.Text = "Please Enter Password";
                SaveStaffID = staffID.Text;
                staffID.Text = "";
            }
            else if (ConfirmLogin == "StaffNotExist")
            {
                TextViewTT.Text = "This User Name Do Not Exist!";
                staffID.Text = "";
            }
        }
        private void CheckStaffPassword()
        {
            NewsteadWebReference.NewsteadService CheckingLogin = new NewsteadWebReference.NewsteadService();
            string loginChecking = CheckingLogin.CheckStaffPassWord(staffID.Text, SaveStaffID);
            if (loginChecking == "PasswordCorrect")
            { 
                StartActivity(typeof(CashierActivity));
            }
            else if (loginChecking == "PasswordIncorrect")
            {
                TextViewTT.Text = "Password Incorrect";
            }
        }
    }

}
